package com.example.testcamara;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.TextureView;

/**
 * @author wupuquan
 */
public class VideoRecordTextureView extends TextureView {
    private int mRatioWidth = 0;
    private int mRatioHeight = 0;

    public VideoRecordTextureView(Context context) {
        this(context, null);
    }

    public VideoRecordTextureView(Context context, AttributeSet atts) {
        this(context, atts, 0);
    }

    public VideoRecordTextureView(Context context, AttributeSet atts, int defStyle) {
        super(context, atts, defStyle);
    }

    public void setAspectRatio(int w, int h) {
        if (w <= 0 || h <= 0) {
            return;
        }
        mRatioWidth = w;
        mRatioHeight = h;
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        if (mRatioWidth == 0 || mRatioHeight == 0) {
            Log.e("EEED", "onMeasure w=" + width + " h=" + height);
            setMeasuredDimension(width, height);
        } else {
            Log.e("EEED", "onMeasure2 w=" + width + " h=" + height);
            setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
        }
    }

}
